<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="85px">
        <el-form-item label="公司编号">
          <el-input
            v-model="searchParam.partnerCode"
            placeholder="请输入公司编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="公司名">
          <el-input
            v-model="searchParam.partnerName"
            placeholder="请输入公司名"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!-- <el-form-item label="机构;基本信息">
                      <el-input
                    v-model="searchParam.organizationId"
                    placeholder="请输入机构;基本信息"
                    clearable
                    @keyup.enter="search"
            />
                  </el-form-item>
        <el-form-item label="机构代码;基本信息">
                      <el-input
                    v-model="searchParam.organizationCode"
                    placeholder="请输入机构代码;基本信息"
                    clearable
                    @keyup.enter="search"
            />
                  </el-form-item>
        <el-form-item label="机构名称;基本信息">
                      <el-input
                    v-model="searchParam.organizationName"
                    placeholder="请输入机构名称;基本信息"
                    clearable
                    @keyup.enter="search"
            />
                  </el-form-item> -->
        <el-form-item label="地址描述">
          <el-input
            v-model="searchParam.addressDesc"
            placeholder="请输入地址描述"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="国家">
          <el-input
            v-model="searchParam.country"
            placeholder="请输入国家"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="省">
          <el-input
            v-model="searchParam.province"
            placeholder="请输入省"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="城市">
          <el-input
            v-model="searchParam.city"
            placeholder="请输入城市"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="地区">
          <el-input
            v-model="searchParam.region"
            placeholder="请输入地区"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="街道">
          <el-input
            v-model="searchParam.street"
            placeholder="请输入街道"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="门牌号">
          <el-input
            v-model="searchParam.houseCode"
            placeholder="请输入门牌号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="邮政编码">
          <el-input
            v-model="searchParam.zipCode"
            placeholder="请输入邮政编码"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="联系人1">
          <el-input
            v-model="searchParam.contactName1"
            placeholder="请输入联系人1"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="联系人1电话">
          <el-input
            v-model="searchParam.contactPhone1"
            placeholder="请输入联系人1电话"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="联系人2">
          <el-input
            v-model="searchParam.contactName2"
            placeholder="请输入联系人2"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="联系人2电话">
          <el-input
            v-model="searchParam.contactPhone2"
            placeholder="请输入联系人2电话"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!-- <el-form-item label="创建时间">
          <el-date-picker
            v-model="searchParam.createTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getcreateTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人">
          <el-input
            v-model="searchParam.createdUserId"
            placeholder="请输入创建人"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="更新时间">
          <el-date-picker
            v-model="searchParam.lastUpdateTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getlastUpdateTime"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人">
          <el-input
            v-model="searchParam.lastUpdatedUserId"
            placeholder="请输入更新人"
            clearable
            @keyup.enter="search"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <!-- v-if="authAllPerm(['npm:mdmcustomer:add'])" -->
        <el-button
          type="danger"
          plain
          :disabled="ids.length === 0"
          @click="handleTableDelete()"
          ><i-ep-delete />删除</el-button
        >
        <!-- v-if="authAllPerm(['npm:mdmcustomer:delete'])" -->
        <!-- <el-button
          class="ml-3"
          @click="
            useDownload('/wms/mdmcustomer/export', '供应商/客户', searchParam)
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        > -->
        <el-button
          type="success"
          @click="excelBoxOpen"
          v-if="authAllPerm(['npm:mdmcustomer:import'])"
          >导入
        </el-button>
        <el-dialog title="供应商/客户数据导入" v-model="excelBox" width="555px">
          <div>
            <el-button
              @click="
                useDownload(
                  '/wms/mdmcustomer/exportTemp',
                  '供应商/客户上传模板',
                  searchParam
                )
              "
            >
              <template #icon>
                <i-ep-download />
              </template>
              供应商/客户上传模板下载
            </el-button>
          </div>
          <div style="margin-top: 10px">
            <input
              class="crop-input"
              type="file"
              name="image"
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
              ref="onload_exl"
              @change="getfilepath"
            />
            <el-button
              @click="upload"
              :disabled="uploadBtn"
              style="cursor: pointer"
              >上传</el-button
            >
          </div>
        </el-dialog>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          label="公司编号"
          prop="partnerCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="公司名"
          prop="partnerName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="机构;基本信息"
          prop="organizationId"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="机构代码;基本信息"
          prop="organizationCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="机构名称;基本信息"
          prop="organizationName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column
          label="地址描述"
          prop="addressDesc"
          min-width="150"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="国家"
          prop="country"
          min-width="60"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="省"
          prop="province"
          min-width="60"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="城市"
          prop="city"
          min-width="60"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="地区"
          prop="region"
          min-width="80"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="街道"
          prop="street"
          min-width="90"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="门牌号"
          prop="houseCode"
          min-width="70"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="邮政编码"
          prop="zipCode"
          min-width="80"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="联系人1"
          prop="contactName1"
          min-width="80"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="联系人1电话"
          prop="contactPhone1"
          min-width="110"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="联系人2"
          prop="contactName2"
          min-width="80"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="联系人2电话"
          prop="contactPhone2"
          min-width="110"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="180"
          show-overflow-tooltip
        >
        </el-table-column>
        <!-- <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="创建人"
          prop="createdUserId"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserId"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="删除标识"
          prop="deleted"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column> -->
        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search /> 查看
            </el-button>
            <!-- v-if="authAllPerm(['npm:mdmcustomer:check'])" -->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <!-- v-if="authAllPerm(['npm:mdmcustomer:edit'])" -->
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <!-- v-if="authAllPerm(['npm:mdmcustomer:delete'])" -->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mdmcustomer-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { authAllPerm } from "@/utils/index";

defineOptions({
  name: "NPMmdmcustomer",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/wms/mdmcustomer/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  partnerCode: "",
  partnerName: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  addressDesc: "",
  country: "",
  province: "",
  city: "",
  region: "",
  street: "",
  houseCode: "",
  zipCode: "",
  contactName1: "",
  contactPhone1: "",
  contactName2: "",
  contactPhone2: "",
  createTimeSearch: [],
  createTimeStart: "",
  createTimeEnd: "",
  createdUserId: "",
  lastUpdateTimeSearch: [],
  lastUpdateTimeStart: "",
  lastUpdateTimeEnd: "",
  lastUpdatedUserId: "",
};

const deleteParam = {
  api: "/wms/mdmcustomer/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/wms/mdmcustomer/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开供应商/客户表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改供应商/客户";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增供应商/客户";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开供应商/客户表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "供应商/客户详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getcreateTime(data: any) {
  if (data) {
    searchParam.value.createTimeStart = data[0];
    searchParam.value.createTimeEnd = data[1];
  } else {
    searchParam.value.createTimeStart = "";
    searchParam.value.createTimeEnd = "";
  }
}
function getlastUpdateTime(data: any) {
  if (data) {
    searchParam.value.lastUpdateTimeStart = data[0];
    searchParam.value.lastUpdateTimeEnd = data[1];
  } else {
    searchParam.value.lastUpdateTimeStart = "";
    searchParam.value.lastUpdateTimeEnd = "";
  }
}

// 导入excel
const uploadApi = "/wms/mdmcustomer/upload";
const excelBox = ref(false);
const uploadBtn = ref(false);
const uploadFile = ref("");
const fileName = ref("");

function excelBoxOpen() {
  excelBox.value = true;
}

function getfilepath(e: any) {
  const file = e.target.files[0];
  fileName.value = file.name;
  uploadFile.value = file;
  uploadBtn.value = false;
}

function upload() {
  if (!uploadFile.value) {
    ElMessage({
      type: "warning",
      message: "请选择文件！",
    });
    return;
  }

  let formdata = new FormData();
  formdata.append("file", uploadFile.value);
  let config = { headers: { "Content-Type": "multipart/form-data" } };
  request
    .post(uploadApi, formdata, config)
    .then((res) => {
      ElMessage({
        type: "success",
        message: "上传成功！",
      });

      excelBox.value = false;
      uploadBtn.value = true;
      search();
    })
    .catch((error) => {
      console.log(error);
    });
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/wms/mdmcustomer/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}
</script>
